package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析logic组件配置文件中“循环开始”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 温建
 * 
 * @version 1.0 2008.8.18
 * 
 * @see <code>StartIteratorNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.StartIteratorNodeInfo;

public class StartIterationNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_ITERATION_START = "start-iteration";

	public static final String KEY_ITERATION_LIST_KEY = "iterator-list-key";

	public static final String KEY_ITERATION_ITEM_KEY = "item-key";

	public static final String KEY_ITERATION_END_ID = "end-id";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> startIterationNodeElements = root
				.elements(KEY_NODETYPE_ITERATION_START);

		List<INodeInfo> startIterationNodeInfos = new ArrayList<INodeInfo>(
				startIterationNodeElements.size());

		for (Element startIterationNodeElement : startIterationNodeElements) {
			String id = startIterationNodeElement.attributeValue(KEY_NODE_ID)
					.trim();

			String x = startIterationNodeElement.attributeValue(KEY_NODE_X).trim();
			String y = startIterationNodeElement.attributeValue(KEY_NODE_Y).trim();
			String nextId = startIterationNodeElement.attributeValue(
					KEY_NODE_NEXT).trim();

			String name = startIterationNodeElement
					.elementTextTrim(KEY_NODE_NAME);

			String description = startIterationNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			String listKey = startIterationNodeElement
					.elementTextTrim(KEY_ITERATION_LIST_KEY);

			String itemKey = startIterationNodeElement
					.elementTextTrim(KEY_ITERATION_ITEM_KEY);

			String endId = startIterationNodeElement
					.elementTextTrim(KEY_ITERATION_END_ID);

			logger.debug("  load iteration node id = \"" + id + "\" name = \"" 
					+ name + "\" description = \"" + description
					+ "\" nextId = \"" + nextId + "\" listKey = \"" 
					+ listKey + "\" itemKey = \"" + itemKey + "\" endId = \"" + endId + "\"");

			StartIteratorNodeInfo startIteratorNodeInfo = new StartIteratorNodeInfo(
					id, name, description, nextId, listKey, itemKey, endId, x, y);

			startIterationNodeInfos.add(startIteratorNodeInfo);
		}
		return startIterationNodeInfos;
	}

}
